package com.followmeandroid.models;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>Title: Member</p>
 * <p>Description: Class for storing information on
 * a member of the application, including names, ids
 * and friends lists.</p>
 * @author Adam Drews
 * @email adam.drews@my.uwrf.edu
 * @date 11/17/2014
 * @team Tyler Johnson
 * @email tyler.johnson@my.uwrf.edu
 * @team Nicholas Thorp
 * @email nicholas.thorp@my.uwrf.edu
 */
public class Member {

    private UUID memberId;
    private String userName;
    private String email;

    //private String password;
    private String lastName;
    private String firstName;
    private String middleInitial;
    private boolean willShareLocation;
    private RegistrationID registrationID = null;
    private List<Friend> friends;
    private List<Message> messages;
    private List<Location> locations;

    /**
     * Default constructor.
     */
    public Member() {}

    /**
     * Constructor - Creates a Member object with a UUID, username, and
     * password as construction parameters.
     * @param id        The UUID of the member.
     * @param uName     The user name of the member.
     * @param pWord     The password of the member.
     */
    public Member(UUID id, String uName, String pWord) {

        this.memberId = id;
        this.userName = uName;
        //this.password = pWord;
        friends = new ArrayList<Friend>();
        willShareLocation = true;
    }

    /**
     * Returns the member's user name.
     * @return userName     The user name of the member.
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Returns a list of locations of the member.
     * @return locations    The user's list of past and present locations.
     */
    public List<Location> getLocations() {
        return locations;
    }

    /**
     * Returns the UUID member id of the member.
     * @return memberId     The member's UUID.
     */
    public UUID getMemberId() {
        return memberId;
    }

    /**
     * Returns the email address of the member.
     * @return email        The member's email address.
     */
    public String getEmail() {
        return email;
    }

    /**
     * Returns the last name of the member.
     * @return lastName     The member's last name.
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Returns the first name of the member.
     * @return firstName    The member's first name.
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Returns the middle initial of the member.
     * @return middleInitial    The member's middle initial.
     */
    public String getMiddleInitial() {
        return middleInitial;
    }

    /**
     * Returns teh registration ID of the member.
     * @return registrationID   The member's registration ID.
     */
    public RegistrationID getRegistrationID() {
        return registrationID;
    }

    /**
     * Sets the registration ID of the member.
     * @param regid         The member's new registration ID.
     */
    public void setRegistrationID(RegistrationID regid) {
        registrationID = regid;
    }

    /**
     * Returns a list of the member's Friends.
     * @return friends      A List of Friend objects, the member's friends..
     */
    public List<Friend> getFriends() {
        return friends;
    }

    /**
     * Returns a list of the member's messages.
     * @return messages     A list of Messages from the member.
     */
    public List<Message> getMessages() {
        return messages;
    }

    /**
     * Sets the member's location to private.
     */
    public void unShareLocation() {
        willShareLocation = false;
    }

    /**
     * Sets the member's location to viewable.
     */
    public void shareLocation() {
        willShareLocation = true;
    }

    /**
     * Returns the status of the member's location availability.
     * @return willShareLocation    Location availability boolean.
     */
    public boolean getWillShareLocation() {
        return willShareLocation;
    }

    /**
     * Attempts to add a Friend to the member's list of Friends.
     * @param friend        The Friend to be added.
     * @return true         Success
     *         false        Error
     */
    public boolean addFriend(Friend friend) {
        if(!isFriendsWith(friend)) {
            this.friends.add(friend);
            return true;
        } return false;
    }

    /**
     * Attempts to delete the given Friend from
     * the member's Friend List.
     * @param friend        The Friend to be deleted.
     * @return  true        Success.
     *          false       Error.
     */
    public boolean deleteFriend(Friend friend){
        int friendLocation = isFriendsWithRemove(friend);
        if(friendLocation != -1){
            this.friends.remove(friendLocation);
            return true;
        }
        return false;
    }

    /**
     * Checks to see if given Friend is in the member's
     * List of Friends.
     * @param friend        The friend to be checked.
     * @return  present     The friend's absence or presence.
     */
    public boolean isFriendsWith(Friend friend){
        boolean present = false;
        for(Friend afr : friends){
            if(afr.getFriendID().compareTo(friend.getFriendID()) == 0) {
                present = true;
                break;
            }
        }

        return present;
    }

    /**
     * Attempts to return the index location of the given friend
     * in the member's list of friends. Returns -1 if the friend
     * is absent from the list.
     * @param friend        The friend to be searched for.
     * @return i            The index of the friend in friends.
     */
    public int isFriendsWithRemove(Friend friend){
        int i=0;
        for(Friend afr : friends){
            if(afr.getFriendID().compareTo(friend.getFriendID()) == 0){
                return i;
            }else {
                i++;
            }
        }
        return -1;
    }

    /**
     * Returns the member's list of friends.
     * @return this.friends     The member's friends list.
     */
    public List<Friend> getListOfFriends() {
        return this.friends;
    }
}
